import pygame


class MyPlane(pygame.sprite.Sprite):  # 飞机类也继承自精灵类
    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)

        # self.image1 = pygame.image.load("images/me1.png").convert_alpha()  # 加载飞机图像1
        # self.image1 = pygame.image.load("images/bluePlane.png").convert_alpha()  # 加载飞机图像1
        self.image1 = pygame.image.load("images/whitePlane.png").convert_alpha()  # 加载飞机图像1
        # self.image2 = pygame.image.load("images/me2.png").convert_alpha()  # 加载飞机图像2
        # self.image2 = pygame.image.load("images/bluePlane.png").convert_alpha()  # 加载飞机图像2
        self.image2 = pygame.image.load("images/whitePlane.png").convert_alpha()  # 加载飞机图像2
        self.destroy_image = []
        # self.destroy_image.extend([ \
        #     pygame.image.load("images/me_destroy_1.png").convert_alpha(), \
        #     pygame.image.load("images/me_destroy_2.png").convert_alpha(), \
        #     pygame.image.load("images/me_destroy_3.png").convert_alpha(), \
        #     pygame.image.load("images/me_destroy_4.png").convert_alpha(), \
        #     ])  # 加载飞机杯摧毁的图像1 2 3 4
        self.destroy_image.extend([ \
            pygame.image.load("images/destroy_1.png").convert_alpha(), \
            pygame.image.load("images/destroy_2.png").convert_alpha(), \
            pygame.image.load("images/destroy_3.png").convert_alpha(), \
            pygame.image.load("images/destroy_4.png").convert_alpha(), \
            pygame.image.load("images/destroy_5.png").convert_alpha(), \
            pygame.image.load("images/destroy_6.png").convert_alpha(), \
            pygame.image.load("images/destroy_7.png").convert_alpha(), \
            pygame.image.load("images/destroy_8.png").convert_alpha(), \
            ])  # 加载飞机杯摧毁的图像1 2 3 4
        self.active = True  # 激活对象
        self.rect = self.image1.get_rect()  # 获取图像大小
        self.width, self.height = bg_size[0], bg_size[1]  # 获取屏幕大小
        self.rect.left, self.rect.top = \
            (self.width - self.rect.width) // 2, \
            self.height - self.rect.height - 60  # 规定生成位置
        self.speed = 10
        self.mask = pygame.mask.from_surface(self.image1)
        self.invincible = False #无敌状态

    def moveUp(self):  # 向上移动
        if self.rect.top > 0:  # 在画布之内
            self.rect.top -= self.speed
        else:  # 否则只能在最上方
            self.rect.top = 0

    def moveDown(self):  # 向下移动
        if self.rect.bottom < self.height - 60:  # 在画布之内
            self.rect.top += self.speed
        else:
            self.rect.bottom = self.height - 60  # 否则只能在最下方 不能出去

    def moveLeft(self):  # 向左移 与上同理
        if self.rect.left > 0:
            self.rect.left -= self.speed
        else:
            self.rect.left = 0

    def moveRight(self):  # 向右移 与上同理
        if self.rect.right < self.width:
            self.rect.left += self.speed
        else:
            self.rect.right = self.width

    def reset(self):  # 重置飞机位置
        self.rect.left, self.rect.top = \
            (self.width - self.rect.width) // 2, \
            self.height - self.rect.height - 60
        self.active = True  # 激活
        self.invincible = True
